<?php
declare (strict_types=1);

namespace app\api\controller;

use addons\WeChatMini\facade\WeChatMini;
use app\common\model\User;
use app\common\model\UserCode;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;

class Login extends Base
{
    public function index(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Login');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        try {
            $user = User::where(['mobile' => $param['mobile']])->where('is_sub', 0)->find();
            if (!$user) throw new \Exception('账号不存在');
            if ($user->password != md5($param['password'])) throw new \Exception('密码错误');
            if ($user->is_lock) throw new \Exception('账号已冻结');
            $time = time();
            $ip = $this->request->ip();
            $token = $user->id == 1 ? 'admin' : md5(json_encode($user) . $time . $ip);
            $user->token = $token;
            $user->last_time = $time;
            $user->last_ip = $ip;
            $user->save();

            return Result::success('登录成功', ['token' => $token]);
        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }
    }

    public function wechatAuth(): Response
    {
        $param = $this->request->post();
        try {
            $data = WeChatMini::session($param['code']);
            if (empty($data['openid'])) {
                throw new \Exception('获取openid失败');
            }
            $param['openid'] = $data['openid'];
            $phoneData = WeChatMini::getPhoneNumber($param['phone_code']);
            if (empty($phoneData['phone_info']['phoneNumber'])) {
                throw new \Exception('获取手机号失败');
            }
            $param['mobile'] = $phoneData['phone_info']['phoneNumber'];

            return Result::fastReturn(User::_miniAppletSave($param));
        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }
    }

    public function verifyCode(): Response
    {
        $param = $this->request->post();
        //校验短信验证码
        $res = UserCode::verifyMsgCode($param['mobile'], $param['captcha'], 'login');
        if ($res !== true) return Result::fail($res);
        try {
            $user = User::where(['mobile' => $param['mobile']])->find();
            if (!$user) {
                throw new \Exception('账号不存在');
            }
            if ($user->is_lock) {
                throw new \Exception('账号已冻结');
            }
            $time = time();
            $ip = $this->request->ip();
            $token = md5(json_encode($user) . $time . $ip);
            $user->token = $token;
            $user->last_time = $time;
            $user->last_ip = $ip;
            $user->save();

            return Result::success('登录成功', ['token' => $token]);
        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }
    }
}